﻿//============================================================
//write by bxl at 2021-06-13 22:08:29
//============================================================



using System.Collections.Generic;
using System;
using BCService.Utils;
using BCCommon;
using BCData.CRM.BigCustomerGroup;
using BCDto.CRM.BigCustomerGroup;
using BCEntity.CRM.BigCustomerGroup;
using BCDto.Sim.Admin.Admin;
using System.Linq;
using BCEntity.Common.EntityNameConst;
using BCService.MapperProfiles.TMS.SpecificServiceType;
using BCDto.TMS.SpecificServiceType;

namespace BCService.CRM.BigCustomerGroup
{
    public class BigCustomerGroupService : IBigCustomerGroupService
    {
        #region var
        private readonly IDatabaseContext databaseContext;
        private IBigCustomerGroupData bigCustomerGroupData;
        private readonly IRedisService redisService;

        private readonly string key = string.Format("{0}", EntityNameConst.BigCustomerGroupEntity);

        #endregion

        #region 构造函数
        public BigCustomerGroupService(IBigCustomerGroupData bigCustomerGroupData, IDatabaseContext databaseContext, IRedisService redisService)
        {
            this.bigCustomerGroupData = bigCustomerGroupData;
            this.databaseContext = databaseContext;
            this.redisService = redisService;
        }
        #endregion

        #region Add
        public BigCustomerGroupDto Add(BigCustomerGroupRequestDto bigCustomerGroupRequestDto, AdminDto admin)
        {
            var list = bigCustomerGroupData.GetOnlyList(admin.CompanyId).GetAwaiter().GetResult().ToList();
            var other = list.FirstOrDefault(s => s.BigCustomerGroupName == bigCustomerGroupRequestDto.BigCustomerGroupName);
            if (other != null)
                throw new ArgumentException("大客户组名重复！");

            var entity = bigCustomerGroupRequestDto.As<BigCustomerGroupEntity>();
            entity.CompanyId = admin.CompanyId;

            entity.CreateAdminId = admin.AdminId;
            entity.CreateAdminName = admin.FullName;
            entity.CreateTime = DateTime.Now;

            return bigCustomerGroupData.InsertAsync(entity).GetAwaiter().GetResult().As<BigCustomerGroupDto>();
        }
        #endregion

        #region update
        public BigCustomerGroupDto Update(BigCustomerGroupPutDto bigCustomerGroupPutDto, AdminDto admin)
        {
            var entity = bigCustomerGroupData.GetEntityAsync(bigCustomerGroupPutDto.BigCustomerGroupId).GetAwaiter().GetResult() ?? throw new ArgumentException("找不到要修改的记录!");

            var list = bigCustomerGroupData.GetOnlyList(admin.CompanyId).GetAwaiter().GetResult().ToList();
            var other = list.FirstOrDefault(s => s.BigCustomerGroupName == bigCustomerGroupPutDto.BigCustomerGroupName);
            if (other != null)
            {
                if (other.BigCustomerGroupId != bigCustomerGroupPutDto.BigCustomerGroupId)
                    throw new ArgumentException("大客户组名重复！");
            }
            entity.BigCustomerGroupName = bigCustomerGroupPutDto.BigCustomerGroupName.Trim();
            entity.GroupRemark = bigCustomerGroupPutDto.GroupRemark.Trim();

            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            entity.UpdateTime = DateTime.Now;

            this.redisService.DeleteMultipleKey(key, false);
            return bigCustomerGroupData.UpdateAsync(entity).GetAwaiter().GetResult().As<BigCustomerGroupDto>();

        }
        #endregion

        #region Delete
        /// <summary>
        /// 删除大客户组记录
        /// </summary>
        /// <param name="bigCustomerGroupId"></param>
        /// <returns></returns>
        public void Delete(long bigCustomerGroupId)
        {
            this.redisService.DeleteMultipleKey(key, false);
            bigCustomerGroupData.DeleteAsync(bigCustomerGroupId).GetAwaiter().GetResult();

        }
        #endregion

        #region GetByBigCustomerGroupId
        public BigCustomerGroupDto GetById(long bigCustomerGroupId)
        {
            string cacheKey = this.redisService.GetKey(key, bigCustomerGroupId);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var a = bigCustomerGroupData.GetEntityAsync(bigCustomerGroupId).GetAwaiter().GetResult();
                return a.As<BigCustomerGroupDto>();
            }
            , TimeSpan.FromDays(1));
            return result.As<BigCustomerGroupDto>();
        }
        #endregion

        #region GetList  获取大客户组记录--[分页]
        public PageableList<BigCustomerGroupDto> GetList(AdminDto admin, string bigCustomerGroupName, int pageIndex, int pageSize)
        {
            int fCount = 0;
            IEnumerable<BigCustomerGroupEntity> fList;

            int? companyId = null;
            if (!admin.IsSystemAccount)
                companyId = admin.CompanyId;


            string cacheKey = this.redisService.GetKey(key, companyId, bigCustomerGroupName, pageIndex, pageSize);

            var result = this.redisService.TryGet(cacheKey, () =>
            {

                (fList, fCount) = bigCustomerGroupData.GetListAsync(companyId, bigCustomerGroupName, pageIndex, pageSize).GetAwaiter().GetResult();

                return new PageableList<BigCustomerGroupDto>()
                {
                    Count = fCount,
                    Items = fList.As<IEnumerable<BigCustomerGroupDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now,
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

        #region GetOnlyList
        public List<BigCustomerGroupDto> GetOnlyList(AdminDto admin)
        {
            string cacheKey = this.redisService.GetKey(key, admin.CompanyId);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return bigCustomerGroupData.GetOnlyList(admin.CompanyId).GetAwaiter().GetResult().ToList().As<List<BigCustomerGroupDto>>();
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

    }
}